package com.kwlt.smc.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.kwlt.smc.dao.BlacklistDao;
import com.kwlt.smc.dao.ChannelDao;
import com.kwlt.smc.dao.KeywordDao;
import com.kwlt.smc.dao.ThemRoughlyDao;
import com.kwlt.smc.dao.UserDao;
import com.kwlt.smc.entity.Blacklist;
import com.kwlt.smc.entity.Channel;
import com.kwlt.smc.entity.Keyword;
import com.kwlt.smc.entity.ThemRoughly;
import com.kwlt.smc.entity.User;

@Component
@Lazy(value=false)
public class CacheMap {
	//1 运营商 2 通道 3 客户 4 其他
	public static final String FROM_OPERATORS="1";
	public static final String FROM_CHANNEL="2";
	public static final String FROM_USER="3";
	public static Logger logger =  LoggerFactory.getLogger(CacheMap.class);
	//全局黑名单
	public static List<Blacklist> globalBlacklist=null;
		//通道黑名单
	public static Map<String,List<Blacklist>> channelMap= new HashMap<String, List<Blacklist>>();
		//用户黑名单
	public static Map<String,List<Blacklist>> userMap= new HashMap<String, List<Blacklist>>();
		//运营商黑名单
	public static Map<String,List<Blacklist>> operatorsMap= new HashMap<String, List<Blacklist>>();
	//用户信息
	public static Map<String,User> userCodeMap = new HashMap<String,User>();
	
	
	//关键字列表
	public static List<Keyword> globalKeyword=null;
	//通道关键字
	public static Map<String,List<Keyword>> channelMapKeyword=null;
	//用户关键字
	public static Map<String,List<Keyword>> userMapKeyword=null;
	//运营商关键字
	public static Map<String,List<Keyword>> operatorsMapKeyword=null;
	//号段缓存信息，内容为phone,prov
	public static Map<String,String> phoneThemRoughlyMap = null;
	
	//通道列表
	public static List<Channel> channelList=new ArrayList<Channel>();
	@Autowired
	private BlacklistDao blacklistDao;
	@Autowired
	private KeywordDao keywordDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ChannelDao channelDao;
	@Autowired
	public ThemRoughlyDao themRoughlyDao;
	
	
	public void setPhoneThemRoughlyMap(){
		logger.info("================themRoughly 加载开始==================");
		phoneThemRoughlyMap = new HashMap<String,String>();
		List<ThemRoughly> list = themRoughlyDao.findList();
		for(ThemRoughly tr:list){
			if(tr!=null){
				if(!StringUtils.isBlank(tr.getPhone()) 
						&& !StringUtils.isBlank(tr.getProvinces())){
					phoneThemRoughlyMap.put(tr.getPhone(), tr.getProvinces());
				}
			}
		}
		logger.info("================themRoughly 加载成功==================");
	}
	
	
	public void setChannelList(){
		 logger.info("================channel 加载开始==================");
		 channelList=channelDao.getChannelList();
		 logger.info("================channel 加载成功==================");
	}
	public void setUserMap(){
		logger.info("================user 加载开始==================");
		List<User> userList=userDao.findAllUser();
		for(User user:userList){
			if(!StringUtils.isBlank(user.getUserNumbers())){
				userCodeMap.put(user.getUserNumbers(), user);
			}
		}
		logger.info("================user 加载成功==================");
	}
	public void setBlackList() {
		 //1 运营商 2 通道 3 客户 4 其他
		logger.info("================blacklist 加载开始==================");
		//加载全部list
		List<Blacklist> allBlacklist=blacklistDao.findAllBlacklist();
		List<Blacklist> operatorsBlacklist =new ArrayList<Blacklist>();
		List<Blacklist> channelBlacklist =new ArrayList<Blacklist>();
		List<Blacklist> userBlacklist =new ArrayList<Blacklist>();
		if(globalBlacklist!=null && globalBlacklist.size()>0){
			globalBlacklist.clear();
		}else{
			globalBlacklist=new ArrayList<Blacklist>();
		}
		for(Blacklist blacklist:allBlacklist){
			if("1".equals(blacklist.getLevel())){
				globalBlacklist.add(blacklist);
			}else if("2".equals(blacklist.getLevel())){
				if(FROM_OPERATORS.equals(blacklist.getSource())){
					operatorsBlacklist.add(blacklist);
				}else if(FROM_CHANNEL.equals(blacklist.getSource())){
					channelBlacklist.add(blacklist);
				}else if(FROM_USER.equals(blacklist.getSource())){
					userBlacklist.add(blacklist);
				}
			}
		}
		operatorsMap=getOperatorsMap(operatorsBlacklist);
		channelMap=getChannelMap(channelBlacklist);
		userMap=getUserMap(userBlacklist);
		logger.info("================blacklist 加载成功==================");
	}
	public void setKeyword() {
		 //1 运营商 2 通道 3 客户 4 其他
		logger.info("================keyword 加载开始==================");
		//加载全部list
		List<Keyword> allKeyword=keywordDao.findAllKeyword();
		List<Keyword> operatorsKeyword =new ArrayList<Keyword>();
		List<Keyword> channelKeyword =new ArrayList<Keyword>();
		List<Keyword> userKeyword =new ArrayList<Keyword>();
		if(globalKeyword!=null && globalKeyword.size()>0){
			globalKeyword.clear();
		}else{
			globalKeyword=new ArrayList<Keyword>();
		}
		for(Keyword keyword:allKeyword){
			if("1".equals(keyword.getLevel())){
				globalKeyword.add(keyword);
			}else if("2".equals(keyword.getLevel())){
				if(FROM_OPERATORS.equals(keyword.getSource())){
					operatorsKeyword.add(keyword);
				}else if(FROM_CHANNEL.equals(keyword.getSource())){
					channelKeyword.add(keyword);
				}else if(FROM_USER.equals(keyword.getSource())){
					userKeyword.add(keyword);
				}
			}
		}
		operatorsMapKeyword=getOperatorsMapKeyword(operatorsKeyword);
		channelMapKeyword=getChannelMapKeyword(channelKeyword);
		userMapKeyword=getUserMapKeyword(userKeyword);
		logger.info("================keyword 加载成功==================");
	}
	
	private Map<String, List<Keyword>> getUserMapKeyword(List<Keyword> userKeyword) {
		Map<String, List<Keyword>> map = new HashMap<String, List<Keyword>>();
		List<Keyword> list=new ArrayList<Keyword>();
		for (int i = 0; i < userKeyword.size(); i++) {
			Keyword keyword = userKeyword.get(i);
			//System.out.println("userCode "+blacklist.toString());
			if (i > 0) {
				Keyword lastKeyword = userKeyword.get(i - 1);
				String lastUserCode = lastKeyword.getUserCode();
				String userCode = keyword.getUserCode();
				if(!lastUserCode.equals(userCode)){
					map.put(lastUserCode, list);
					list=new ArrayList<Keyword>();
				}
			}
			list.add(keyword);
		}
		if(list.size()>0){
			map.put(list.get(0).getUserCode(), list);
		}
		return map;
	}
	private Map<String, List<Keyword>> getChannelMapKeyword(List<Keyword> channelKeyword) {
		Map<String, List<Keyword>> map = new HashMap<String, List<Keyword>>();
		List<Keyword> list=new ArrayList<Keyword>();
		for (int i = 0; i < channelKeyword.size(); i++) {
			Keyword keyword = channelKeyword.get(i);
			if (i > 0) {
				Keyword lastKeyword = channelKeyword.get(i - 1);
				String lastChannelCode = lastKeyword.getChannelCode();
				String channelCode = keyword.getChannelCode();
				if (!lastChannelCode.equals(channelCode)) {
					map.put(lastChannelCode, list);
					list=new ArrayList<Keyword>();
				}
			}
			list.add(keyword);
		}
		if(list.size()>0){
			map.put(list.get(0).getChannelCode(), list);
		}
		return map;
	}
	private Map<String, List<Keyword>> getOperatorsMapKeyword(List<Keyword> operatorsKeyword) {
		Map<String, List<Keyword>> map = new HashMap<String, List<Keyword>>();
		List<Keyword> list=new ArrayList<Keyword>();
		for (int i = 0; i < operatorsKeyword.size(); i++) {
			Keyword keyword = operatorsKeyword.get(i);
			//System.out.println("operators "+blacklist.toString());
			if (i > 0) {
				Keyword lastKeyword = operatorsKeyword.get(i - 1);
				String lastOperators = lastKeyword.getOperators();
				String operators = keyword.getOperators();
				if (!lastOperators.equals(operators)) {
					map.put(lastOperators, list);
					list=new ArrayList<Keyword>();
				}
			}
			list.add(keyword);
		}
		if(list.size()>0){
			map.put(list.get(0).getOperators(), list);
		}
		return map;	
	}
	private Map<String, List<Blacklist>> getUserMap(List<Blacklist> blacklistList) {
		Map<String, List<Blacklist>> map = new HashMap<String, List<Blacklist>>();
		List<Blacklist> list=new ArrayList<Blacklist>();
		for (int i = 0; i < blacklistList.size(); i++) {
			Blacklist blacklist = blacklistList.get(i);
			//System.out.println("userCode "+blacklist.toString());
			if (i > 0) {
				Blacklist lastBlacklist = blacklistList.get(i - 1);
				String lastUserCode = lastBlacklist.getUserCode();
				String userCode = blacklist.getUserCode();
				if(!lastUserCode.equals(userCode)){
					map.put(lastUserCode, list);
					list=new ArrayList<Blacklist>();
				}
			}
			list.add(blacklist);
		}
		if(list.size()>0){
			map.put(list.get(0).getUserCode(), list);
		}
		return map;
	}
	private Map<String, List<Blacklist>> getChannelMap(List<Blacklist> blacklistList) {
		Map<String, List<Blacklist>> map = new HashMap<String, List<Blacklist>>();
		List<Blacklist> list=new ArrayList<Blacklist>();
		for (int i = 0; i < blacklistList.size(); i++) {
			Blacklist blacklist = blacklistList.get(i);
			if (i > 0) {
				Blacklist lastBlacklist = blacklistList.get(i - 1);
				String lastChannelCode = lastBlacklist.getChannelCode();
				String channelCode = blacklist.getChannelCode();
				if (!lastChannelCode.equals(channelCode)) {
					map.put(lastChannelCode, list);
					list=new ArrayList<Blacklist>();
				}
			}
			list.add(blacklist);
		}
		if(list.size()>0){
			map.put(list.get(0).getChannelCode(), list);
		}
		return map;
	}

	private Map<String, List<Blacklist>> getOperatorsMap(List<Blacklist> blacklistList) {
		Map<String, List<Blacklist>> map = new HashMap<String, List<Blacklist>>();
		List<Blacklist> list=new ArrayList<Blacklist>();
		for (int i = 0; i < blacklistList.size(); i++) {
			Blacklist blacklist = blacklistList.get(i);
			//System.out.println("operators "+blacklist.toString());
			if (i > 0) {
				Blacklist lastBlacklist = blacklistList.get(i - 1);
				String lastOperators = lastBlacklist.getOperators();
				String operators = blacklist.getOperators();
				if (!lastOperators.equals(operators)) {
					map.put(lastOperators, list);
					list=new ArrayList<Blacklist>();
				}
			}
			list.add(blacklist);
		}
		if(list.size()>0){
			map.put(list.get(0).getOperators(), list);
		}
		return map;
	}
	public  void updateKeywordCache(){
		setKeyword();
	}
	public  void updateBlackCache(){
		setBlackList();
	}
	public void updateUserCache(){
		setUserMap();
	}
	public void updateChannelCache(){
		setChannelList();
	}
	
	public void updatePhoneThem(){
		setPhoneThemRoughlyMap();
	}
}
